<?php

/**
* 模拟服务器响应
*/
class Server {
	
    /* 基本演示 */
	public function example() {
        //请求类型
        echo '<hr>[REQUEST_METHOD]<br>';
        echo $_SERVER['REQUEST_METHOD'];
		//请求来源referer
        if (!empty($_SERVER['HTTP_REFERER'])) {
            echo '<hr>[HTTP_REFERER]<br>';
            echo $_SERVER['HTTP_REFERER'];
        }
		//请求来源ip
		//[注]此处的IP打印顺序是目前很多开源系统的IP获取顺序 
		echo '<hr>[IP]<br>';
		echo $_SERVER['HTTP_CLIENT_IP'];
		echo '<br>';
		echo $_SERVER['HTTP_X_FORWARDED_FOR'];
		echo '<br>';
		echo $_SERVER['REMOTE_ADDR'];
		//请求来源agent
        if (!empty($_SERVER['HTTP_USER_AGENT'])) {
            echo '<hr>[HTTP_USER_AGENT]<br>';
            echo $_SERVER['HTTP_USER_AGENT'];
        }
        //GET数据
        if (!empty($_GET)) {
            echo '<hr>[GET]<br><pre>';
            var_dump($_GET);
            echo '</pre>';
        }
		//POST数据
        if (!empty($_POST)) {
            echo '<hr>[POST]<br><pre>';
            var_dump($_POST);
            echo '</pre>';
        }
		//当前所有cookie
        if (!empty($_COOKIE)) {
            echo '<hr>[COOKIE]<br><pre>';
            var_dump($_COOKIE);
            echo '</pre><hr>';
        }
        //后台设置两个cookie
        setcookie("name", "feifei", time()+36);
        setcookie("age", "18", time()+36);
	}

	/* 获得访客浏览器类型 */
    function getBrowser(){
        if(!empty($_SERVER['HTTP_USER_AGENT'])) {
            $br = $_SERVER['HTTP_USER_AGENT'];
            if (preg_match('/MSIE/i',$br)) {   
                $br = 'MSIE';
            }elseif (preg_match('/Firefox/i',$br)) {
                $br = 'Firefox';
            }elseif (preg_match('/Chrome/i',$br)) {
                $br = 'Chrome';
            }elseif (preg_match('/Safari/i',$br)) {
                $br = 'Safari';
            }elseif (preg_match('/Opera/i',$br)) {
                $br = 'Opera';
            }elseif (preg_match('/QQBrowser/i',$br)) {
                $br = 'QQBrowser';
            }elseif (preg_match('/UCBrowser/i',$br)) {
                $br = 'UCBrowser';
            }else {
                $br = 'Other';
            }
            return $br;
        }else {
            return false;
        }
    }

    /* 获取访客操作系统 */
    function getOs(){
        if(!empty($_SERVER['HTTP_USER_AGENT'])) {
            $OS = $_SERVER['HTTP_USER_AGENT'];
            if (strpos($OS, 'Android') !== false) {//strpos()定位出第一次出现字符串的位置，这里定位为0 
                preg_match("/(?<=Android )[\d\.]{1,}/", $OS, $version); 
                $OS ='Android '.$version[0];
            }elseif (strpos($OS, 'iPhone') !== false) { 
                preg_match("/(?<=CPU iPhone OS )[\d\_]{1,}/", $OS, $version); 
                $OS = 'iPhone iOS '.str_replace('_', '.', $version[0]); 
            }elseif (strpos($OS, 'iPad') !== false) { 
                preg_match("/(?<=CPU OS )[\d\_]{1,}/", $OS, $version); 
                $OS = 'iPad iOS '.str_replace('_', '.', $version[0]);  
            }elseif (preg_match('/win/i',$OS)) {
                $OS = 'Windows';
            }elseif (preg_match('/mac/i',$OS)) {
                $OS = 'MAC';
            }elseif (preg_match('/linux/i',$OS)) {
                $OS = 'Linux';
            }elseif (preg_match('/unix/i',$OS)) {
                $OS = 'Unix';
            }elseif (preg_match('/bsd/i',$OS)) {
                $OS = 'BSD';
            }else {
                $OS = 'Other';
            }
            return $OS; 
        }else {
            return false;
        }  
    }

    /* 获得访客真实ip */
    function getIp() {
        $ip = false;
        if (getenv("HTTP_CLIENT_IP")) {
            $ip = getenv("HTTP_CLIENT_IP");
        }elseif (getenv("HTTP_X_FORWARDED_FOR")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }elseif (getenv("REMOTE_ADDR")) {
            $ip = getenv("REMOTE_ADDR");
        }
        return $ip;
    }

    /* 根据ip获得访客地名 */
    function getAddr($ip='') {
        if (empty($ip)) {
        	$ip = $this->getIp();
	    }
	    if ($ip == '0.0.0.0' || $ip == '127.0.0.1' || $ip == '::1') {
	        return "本地";
	    }
	    $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);//根据新浪api接口获取
	    if($ipadd) {
	        $charset = iconv("gbk","utf-8",$ipadd);  
	        $str = str_replace('1', '', $charset);
	        $str = str_replace('-', '', $str);
	        $str = trim($str);
	        return $str;
	    }else {
	        return "未知";
	    }
    }

}

$Server = new Server();
$Server->example();
echo '<hr>'.$Server->getAddr();